package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Re;
import com.example.dto.SetmealDto;
import com.example.entity.Setmeal;
import com.example.service.ISetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 套餐
 * </p>
 *
 * @author czx
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Resource
    private ISetmealService setmealService;

    /**
     * 添加套餐
     *
     * @param setmealDto 数据传输对象
     * @return 结果
     */
    @PostMapping
    @CacheEvict(value = "setmealCache", key = "#setmealDto.categoryId + '_1'", allEntries = true)
    public Re<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐添加 --> 套餐名 = {}", setmealDto.getName());
        boolean save = setmealService.saveWithSetmealDish(setmealDto);

        if (save) {
            return Re.success("套餐添加成功");
        }
        return Re.error("套餐添加失败");
    }

    /**
     * 套餐分页查询
     *
     * @param page     页数
     * @param pageSize 页大小
     * @param name     套餐名称
     * @return 结果
     */
    @GetMapping("/page")
    public Re<Page<SetmealDto>> page(int page, int pageSize, String name) {
        log.info("套餐分页查询中，name = {}", name);
        Page<SetmealDto> pageInfo = setmealService.page(page, pageSize, name);
        return Re.success(pageInfo);
    }

    /**
     * 套餐信息回显
     *
     * @param id 套餐id
     * @return 结果
     */
    @GetMapping("/{id}")
    public Re<Setmeal> getByIdWithSetmealDish(@PathVariable Long id) {
        log.info("套餐信息回显中，id = {}", id);
        Setmeal setmeal = setmealService.getByIdWithSetmealDish(id);
        if (setmeal == null) {
            return Re.error("数据为空");
        }
        return Re.success(setmeal);
    }

    /**
     * 套餐信息修改
     *
     * @param setmealDto 数据传输对象
     * @return String结果
     */
    @PutMapping
    @CacheEvict(value = "setmealCache", key = "#setmealDto.categoryId + '_1'", allEntries = true)
    public Re<String> update(@RequestBody SetmealDto setmealDto) {
        log.info("修改套餐；{}", setmealDto.getName());
        boolean update = setmealService.updateWithSetmealDish(setmealDto);
        if (update) {
            return Re.success("修改套餐成功");
        }
        return Re.error("修改套餐失败");
    }

    /**
     * 套餐状态修改
     *
     * @param status 修改后的套餐状态
     * @param ids    (一或多)套餐的id
     * @return String结果类
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Re<String> updateByStatus(@PathVariable int status, Long[] ids) {
        log.info("套餐状态修改:{}", status);

        boolean update = setmealService.updateByStatus(status, ids);
        if (!update) return Re.error("套餐状态修改失败");

        return Re.success("套餐状态修改成功");
    }

    /**
     * 套餐删除
     *
     * @param ids 套餐的id
     * @return 结果
     */
    @DeleteMapping
    public Re<String> delete(@RequestParam List<Long> ids) {
        log.info("套餐删除: {}", ids);
        boolean del = setmealService.removeWithSetmealDish(ids);
        if (del) {
            return Re.success("套餐删除成功");
        }
        return Re.error("套餐删除失败");
    }

    /**
     * 套餐列表
     *
     * @param setmeal 套餐查询条件
     * @return 结果
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status",cacheManager = "redisCacheManager30M")
    public Re<List<Setmeal>> list(Setmeal setmeal) {
        log.info("套餐列表信息查询：{}", setmeal.toString());
        List<Setmeal> setmealList = setmealService.listByIdAndStatus(setmeal);
        if (setmealList != null) {
            return Re.success(setmealList);
        }
        return Re.error("套餐查询失败");
    }

    /**
     * 套餐信息获取
     *
     * @param id 套餐id
     * @return 结果
     */
    @GetMapping("/dish/{id}")
    public Re<Setmeal> getById(@PathVariable Long id) {
        log.info("套餐信息获取，id = {}", id);
        Setmeal setmeal = setmealService.getById(id);
        if (setmeal == null) {
            return Re.error("数据为空");
        }
        return Re.success(setmeal);
    }
}
